package com.ruoyi.hcrf.enums;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @BelongsProject: ktitScaffolding-server-projects
 * @BelongsPackage: com.ktit.common.api
 * @Author: Xzj
 * @CreateTime: 2022-10-18 14:55
 * @Description: 通用返回对象
 */
public class CommonResultT<T> {
    private long code;
    private String message;
    private String createTime;

    private T data;

//    //年月
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static Date date = new Date();
    private static  String format = dateFormat.format(date);


    protected CommonResultT() {
    }

    protected CommonResultT(long code, String message, T data, String createTime) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.createTime=createTime;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> CommonResultT<T> success(T data) {
        return new CommonResultT<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data,format);
    }

    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     */
    public static <T> CommonResultT<T> success(T data, String message) {
        return new CommonResultT<T>(ResultCode.SUCCESS.getCode(), message, data,format);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static <T> CommonResultT<T> failed(IErrorCode errorCode) {
        return new CommonResultT<T>(errorCode.getCode(), errorCode.getMessage(), null,format);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     * @param message   错误信息
     */
    public static <T> CommonResultT<T> failed(IErrorCode errorCode, String message) {
        return new CommonResultT<T>(errorCode.getCode(), message, null,format);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> CommonResultT<T> failed(String message) {
        return new CommonResultT<T>(ResultCode.FAILED.getCode(), message, null,format);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResultT<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResultT<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> CommonResultT<T> validateFailed(String message) {
        return new CommonResultT<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null,format);
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResultT<T> unauthorized(T data) {
        return new CommonResultT<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data,format);
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResultT<T> forbidden(T data) {
        return new CommonResultT<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data,format);
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }
}
